package com.video.main_video.manager.handle.uploadFile;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.video.main_video.common.jwt.JwtUtils;
import com.video.main_video.dao.entity.User;
import com.video.main_video.dao.entity.VideoInfo;
import com.video.main_video.dao.request.userInfo.UserVideoRequest;
import com.video.main_video.manager.handle.ApiHandler;
import com.video.main_video.manager.mapper.VideoInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class VideoGet{

    @Autowired
    VideoInfoMapper videoInfoMapper;

    public void handler(String videoSrc, HttpServletRequest request, HttpServletResponse response){
        DecodedJWT verify = JwtUtils.verify(videoSrc);
        if (verify==null){
            return;
        }
        OutputStream outputStream = null;
        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;
        byte[] bytes = new byte[1024];
        String path=verify.getClaim("path").asString()+"\\"+verify.getClaim("videoName").asString();
        File file = new File(path);
        String fileName = file.getName();

        try {
            String var10002 = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            response.setHeader("Content-Disposition", "attachment;filename=" + var10002);
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.setContentType("application/octet-stream;charset=utf-8");
            inputStream = new FileInputStream(file);
            bufferedInputStream = new BufferedInputStream(inputStream);
            outputStream = response.getOutputStream();

            for(int i = bufferedInputStream.read(bytes); i != -1; i = bufferedInputStream.read(bytes)) {
                outputStream.write(bytes, 0, i);
            }
        } catch (IOException var19) {
            var19.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }

                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
            } catch (IOException var18) {
                var18.printStackTrace();
            }
        }
    }

    private boolean checkUserId(String userId,String token){

        DecodedJWT verify = JwtUtils.verify(token);
        String id = null;
        if (verify != null) {
            id = verify.getClaim("userId").asString();
        }
        return Objects.equals(userId, id);
    }

    private VideoInfo getVideo(UserVideoRequest req){
        LambdaQueryWrapper<VideoInfo> qw = new LambdaQueryWrapper<VideoInfo>();
        qw.eq(VideoInfo::getUserId,req.getUserId()).eq(VideoInfo::getCutId,req.getCutId());
        return videoInfoMapper.selectOne(qw);
    }
}
